<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/5
 * Time: 11:06
 */

namespace app\api\controller;

use think\Db;
use think\Exception;

class Test {

    public function testUser() {
//        $begin = 11;
//        $end = 100;
//        for($i = $begin; $i <= $end; $i++){
//            $member = Db::query("select id, parentid, team, route, depth from pi_user where id = {$i}");
//            if( ! $member){
//                continue;
//            }
//            $sql = "update pi_user set route = concat('{$member[0]['route']}', id, ','), depth = {$member[0]['depth']} + 1 where parentid = {$member[0]['id']}";
//            Db::execute($sql);
//            $sql = "update pi_user set team = team + 1 where id in (0{$member[0]['route']}0)";
//            Db::execute($sql);
//        }
//        echo 'success', $begin;
        
        $member = Db::query("select id, parentid, team, route, depth from pi_user where route is null");
        if( ! $member){
            die('over');
        }
        foreach($member as $val){
            $recommend = Db::query("select id, parentid, team, route, depth from pi_user where id = {$val['parentid']}");
            $sql = "update pi_user set route = concat('{$recommend[0]['route']}', id, ','), depth = {$recommend[0]['depth']} + 1, team = 1 where id = {$val['id']}";
            Db::execute($sql);
            $sql = "update pi_user set team = team + 1 where id in (0{$recommend[0]['route']}0)";
            Db::execute($sql);
        }
    }

}
